﻿namespace LeadMaster.Controllers
{
    using System;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;
    using LeadMaster.DAL;
    using LeadMaster.ViewModels;
    using System.Web.Security;
    using LeadMaster.Helpers;
    using System.Collections.Generic;
    using LeadMaster.Models;
    using System.Data;
    using LeadMaster.Properties;
    using System.Configuration;
    using System.Web;
    using System.IO;
    using System.Text;
    using System.Data.Entity.Validation;
    using System.Diagnostics;

    public class MarketingEnquiriesController : Controller
    {

        private DunsmoreContext db = new DunsmoreContext();

        public ActionResult Index()
        {
            Maintenance.inMaintenance = true;
            ViewModelMarketingEnquiries ViewModel = new ViewModelMarketingEnquiries();

            ViewModel.Sectors = this.db.Sectors;
            ViewModel.Salutations = this.db.Salutations;

            return PartialView(ViewModel);
        }

        [HttpPost]
        public void Index(ViewModelMarketingEnquiries ViewModel)
        {
            try
            {
            Companies company = new Companies();
            company.Company = ViewModel.Company;
            company.CoNumber=ViewModel.CompanyRegistrationNumber;
            company.Address1 = ViewModel.Address1;
            company.Address2 = ViewModel.Address2;
            company.District=ViewModel.District;
            company.Town = ViewModel.Town;
            company.County = ViewModel.County;
            company.Postcode = ViewModel.Postcode;
            company.Telephone = ViewModel.Telephone;
            company.Email = ViewModel.Email;
            db.Companies.Add(company);
            TradingAddresses tradingAddress = new TradingAddresses();
            tradingAddress.Company = company;
            tradingAddress.TradingAddress1 = ViewModel.Address1;
            tradingAddress.TradingAddress2 = ViewModel.Address2;
            tradingAddress.TradingTown = ViewModel.Town;
            tradingAddress.TradingDistrict = ViewModel.District;
            tradingAddress.TradingCounty = ViewModel.County;
            tradingAddress.TradingPostcode = ViewModel.Postcode;
            db.TradingAddresses.Add(tradingAddress);
            Contacts contact = new Contacts();
            contact.Company = company;
            contact.Forename = ViewModel.Forename;
            contact.Surname = ViewModel.Surname;
            contact.Telephone = ViewModel.Telephone;
            contact.Email = ViewModel.Email;
            contact.SalutationID = ViewModel.SalutationID;
            db.Contacts.Add(contact);
            Leads lead = new Leads();
            lead.Contact = contact;
            lead.Company = company;
            lead.FreeNotes = ViewModel.Notes;
            lead.TotalWeeks = 1;
            lead.InitialContactDate = DateTime.Now;
            lead.CommencementDate = DateTime.Now;
            lead.LeadStatusesID = 1;
            lead.SectorID = ViewModel.SectorID;
            lead.LastActionBy = (Guid)Membership.GetUser().ProviderUserKey;
            lead.UserID = (Guid)Membership.GetUser().ProviderUserKey;
            lead.CallCentre1ID = 1;
            lead.ScheduleTemplateID = 3;
            lead.LeadsReplacementID = db.LeadsReplacement.First().LeadsReplacementID;
            db.Leads.Add(lead);
            Enquiries enquiry = new Enquiries();
            enquiry.Lead = lead;
            enquiry.Notes = "Initial Contact." + ViewModel.Notes;
            enquiry.Company = company;
            enquiry.Contact = contact;
            enquiry.EnquiryTypeID = 1; // to be fulfulled at a later date
            enquiry.UserID = (Guid)Membership.GetUser().ProviderUserKey;
            enquiry.EnquiryDate = DateTime.Now;
            db.Enquiry.Add(enquiry);
            db.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }

        public ActionResult TestPayPal()
        {

            return View();
        }
    }
}
